var utils = require("../../utils/utils");
var ocrGml = require("../../utils/ocrGml");
var nextVideo = require("../appCommon/nextVideo");
var common = require("./common");
var ACTION = require("../../base/action");
var actionRead = new ACTION("看视频", {
  interval: null, //每个多少秒执行一次
  canStartHour: null, //最早可以开始的时间
  canStartHourEnd: null, //最晚可以开始的时间
  lastStartTime: null, //上一次的开始时间
  lastEndTime: null, //上一次的结束时间
  execSCount: 0, //执行成功次数
  execFCount: 0, //执行失败次数
  maxExecSCount: null, //最大成功执行次数
  maxExecFCount: null, //最大失败执行次数
  maxTotalExecCount: null, //最大总执行次数
  storage: common.storage,
});

actionRead.read = function () {
  console.show();
  var content = "点击翻倍";
  var ocrItem = ocrGml.captureExistOne(content, true);
  if (ocrItem) {
    utils.clickOcrItem(ocrItem);
    sleep(utils.getRandom(2, 3, 1000));
  }
  nextVideo();
  var execSCount = this.getStorage().execSCount || 0;
  console.log("已经运行-" + execSCount + "-次");
  var baseSpan = 6 + execSCount > 10 ? 10 : execSCount;
  var span = utils.getRandom(baseSpan, baseSpan + 2, 1);
  for (var index = 0; index < span; index++) {
    var baseInterval = 8;
    var interval = utils.getRandom(baseInterval, baseInterval + 4);
    console.log(
      this.name,
      "read",
      "看第-" + (index + 1) + "/" + span + "-个视频，将看-" + interval + "-s"
    );
    var jumpVideo = this.checkIfJumpVideo(interval);
    if (jumpVideo) {
      index = index - 1;
      continue;
    }
    if (index < span - 1) {
      nextVideo();
    }
  }
  console.hide();
};

actionRead.checkIfJumpVideo = function (interval) {
  var hasWatch = 0;
  var hasCheck = 0;
  while (hasWatch < interval) {
    var baseWait = 3;
    var wait = utils.getRandom(baseWait, baseWait + 3);
    sleep(wait * 1000);
    var contents = ["直播间"];
    if (hasCheck < 1 && ocrGml.captureExistOne(contents, true)) {
      sleep(utils.getRandom(1, 1.5, 1000));
      nextVideo();
      return true;
    }
    hasCheck = hasCheck + 1;
    hasWatch = hasWatch + wait;
  }
  return false;
};

actionRead.exec = function () {
  this.read();
  this.execSCount++;
  this.updateStorage();
};

module.exports = actionRead;
